package com.chd.driversystem.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.chd.driversystem.R;
import com.chd.driversystem.activity.msgbean.NetMsg;
import com.chd.driversystem.domain.User;
import com.chd.driversystem.gen.BeanServer;
import com.chd.driversystem.service.HttpService;
import com.chd.driversystem.util.network.tools.HttpConstants;
import com.chd.driversystem.util.tools.LogUtil;
import com.chd.driversystem.util.tools.MyApplication;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import lib.kingja.switchbutton.SwitchMultiButton;

public class SignUpActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "SignUpActivity";

    @BindView(R.id.ibtn_back)
    ImageButton ibtnBack;

    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_rpassword)
    EditText etRpassword;
    @BindView(R.id.btn_sign_up)
    Button btnSignUp;

    @BindView(R.id.smb_switchrange)
    SwitchMultiButton smbSwitchJob;

    private Integer pos;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        ButterKnife.bind(this);

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        initView();
    }

    private void initView() {
        ibtnBack.setOnClickListener(this);
        btnSignUp.setOnClickListener(this);

        smbSwitchJob.setText(Arrays.asList("管理员", "司机")).setOnSwitchListener(new SwitchMultiButton.OnSwitchListener() {
            @Override
            public void onSwitch(int position, String tabText) {
                Log.d(TAG, "onSwitch() called with: position = [" + position + "], tabText = [" + tabText + "]");
                pos = position;
                switch (position) {
                    case 0:

                        break;
                    case 1:

                        break;
                    default:
                        break;
                }
            }
        });
        pos = 0;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_back:
                finish();
                break;
            case R.id.btn_sign_up:
                String loginname = etUsername.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                String rPassword = etRpassword.getText().toString().trim();

                if (!TextUtils.isEmpty(loginname) && !TextUtils.isEmpty(loginname) && !TextUtils.isEmpty(loginname)) {
                    if (password.equals(rPassword)) {
                        HttpService.startServiceSignUp(
                                MyApplication.getContext(),
                                loginname,
                                password,
                                pos
                        );
                    } else {
                        Toast.makeText(this, "密码不一致", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(this, "用户名与密码必填", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, SignUpActivity.class);
        context.startActivity(intent);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(NetMsg netMsg){
        if (netMsg.getType() == NetMsg.MAIN) {
            switch (netMsg.getTAG()) {
                case HttpConstants.ACTION_SIGNUP:
                    LogUtil.d(TAG, "receiverNetMsg() called with: netMsg = [" + netMsg + "]");
                    Toast.makeText(this,netMsg.getERR_MSG(),Toast.LENGTH_SHORT).show();
                    if (netMsg.getRESULT().equals(HttpConstants.RESULT_OK)) {
                        finish();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void receiverNetMsg(NetMsg netMsg) {
        if (netMsg.getType() == NetMsg.BACKGROUND) {
            switch (netMsg.getTAG()) {
                case HttpConstants.ACTION_SIGNUP:
                    LogUtil.d(TAG, "receiverNetMsg() called with: netMsg = [" + netMsg + "]");
                    if (netMsg.getRESULT().equals(HttpConstants.RESULT_OK)) {
                        if (netMsg.getId() > 0) {
                            LogUtil.d(TAG, "receiverNetMsg() id = " + netMsg.getId());
                            netMsg.setERR_MSG("登录成功");
                        }else {
                            netMsg.setRESULT(HttpConstants.RESULT_ERR);
                            netMsg.setERR_MSG("id为负数");
                        }
                    } else {
                        //提示错误信息
                    }
                    netMsg.setType(NetMsg.MAIN);
                    break;
                default:
                    break;
            }
        }
    }
}
